import { PaginationResponse } from '@/common';
import { StoreEdgeItemType } from '../workflow/edge';
import { ParentIdType, StoreNodeItemType } from '../workflow/node';
import {
  AppChatConfigType,
  AppSchema,
  AppTypeEnum,
  PublishTypeEnum,
  TeamAppListItemType,
} from './type';
import {
  compressBase64Img,
  CompressImgProps,
  preUploadImgProps,
  UploadImgProps,
} from '@/pages/chat/tools/constants';
import appService from '@/api/services/appService';

export type ListAppBody = {
  parentId?: ParentIdType;
  type?: AppTypeEnum | AppTypeEnum[];
  getRecentlyChat?: boolean;
  searchKey?: string;
};

export type AppBasicInfo = {
  app_name: string;
  app_description: string;
  app_logo: string;
  scene_image: string;
};

export type AppUpdateParams = {
  parentId?: ParentIdType;
  name?: string;
  type?: AppTypeEnum;
  avatar?: string;
  intro?: string;
  nodes?: AppSchema['modules'];
  edges?: AppSchema['edges'];
  chatConfig?: AppSchema['chatConfig'];
  teamTags?: AppSchema['teamTags'];
};

export type PostPublishAppProps = {
  type: AppTypeEnum;
  nodes: AppSchema['modules'];
  edges: AppSchema['edges'];
  chat_config: AppSchema['chatConfig'];
  is_publish?: boolean;
  version_name?: string;
  publishType?: PublishTypeEnum;
};

export type GetLatestVersionResponse = {
  nodes: StoreNodeItemType[];
  edges: StoreEdgeItemType[];
  chatConfig: AppChatConfigType;
};

export type VersionListItemType = {
  _id: string;
  appId: string;
  versionName: string;
  time: Date;
  isPublish: boolean | undefined;
  tmbId: string;
};

export type AppVersionSchemaType = {
  _id: string;
  appId: string;
  time: Date;
  nodes: AppSchema['modules'];
  edges: AppSchema['edges'];
  chatConfig: AppSchema['chatConfig'];
  isPublish?: boolean;
  versionName: string;
  tmbId: string;
  app?: AppSchema;
};

export type VersionListResponse = PaginationResponse<VersionListItemType>;

export type CreateAppBody = {
  parentId?: ParentIdType;
  name?: string;
  avatar?: string;
  type?: AppTypeEnum;
  modules: AppSchema['modules'];
  edges?: AppSchema['edges'];
  chatConfig?: AppSchema['chatConfig'];
};

export type UpdateAppVersionBody = {
  appId: string;
  versionId: string;
  versionName: string;
};

export type TeamAppListResponse = {
  app_list: TeamAppListItemType[];
};

export const compressBase64ImgAndUpload = async ({
  base64Img,
  maxW,
  maxH,
  maxSize,
  ...props
}: UploadImgProps & CompressImgProps) => {
  // const compressUrl = await compressBase64Img({
  //   base64Img,
  //   maxW,
  //   maxH,
  //   maxSize,
  // });

  return appService.postUploadImage({
    ...props,
    base64Img: base64Img,
  });
};

export const compressImgFileAndUpload = async ({
  file,
  ...props
}: preUploadImgProps &
  CompressImgProps & {
    file: File;
  }) => {
  const reader = new FileReader();
  reader.readAsDataURL(file);

  const base64Img = await new Promise<string>((resolve, reject) => {
    reader.onload = async () => {
      resolve(reader.result as string);
    };
    reader.onerror = (err) => {
      console.log(err);
      reject('Load image error');
    };
  });

  return compressBase64ImgAndUpload({
    base64Img,
    ...props,
  });
};
